<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell >
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ artObj.title }}</span>
          <!-- 单图 -->
          <img
            class="thumb"
            v-lazy="artObj.cover.images[0]"
            v-if="artObj.cover.type === 1"
          />
          <!-- <van-image
            class="thumb"
            :src="artObj.cover.images[0]"
            v-if="artObj.cover.type === 1"
          >
            <template v-slot:error>图片走丢了</template>
          </van-image> -->
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="artObj.cover.type > 1">
          <img
            class="thumb"
            v-for="(imgUrl, index) in artObj.cover.images"
            :key="index"
            v-lazy="imgUrl"
          />
          <!-- <van-image
            class="thumb"
            v-for="(imgUrl, index) in artObj.cover.images"
            :key="index"
            :src="imgUrl"
          >
            <template v-slot:error>图片走丢了</template>
          </van-image> -->
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ artObj.aut_name }}</span>
            <span>{{ artObj.comm_count }}评论</span>
            <span>{{ formatTime(artObj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="show = true" v-if="isShow" />
        </div>
      </template>
    </van-cell>
    <van-action-sheet
      v-model="show"
      :actions="actions"
      @select="onSelect"
      get-container="body"
      :cancel-text="cancelText"
      @cancel="onCancel"
      @close="onClose"
    />
  </div>
</template>

<script>
import { timeAgo } from '@/utils/date'
import { firstActions, secondActions } from '@/api/report'
export default {
  data () {
    return {
      show: false, // 反馈面板显示/隐藏
      actions: firstActions, // 选项数据
      cancelText: '取消' // 底部按钮文字
    }
  },
  props: {
    artObj: Object, // 文章对象
    isShow: {
      type: Boolean,
      default: true // 控制反馈首页默认显示
    }
  },
  methods: {
    formatTime: timeAgo, // 时间格式化函数
    // 反馈- 选项事件
    onSelect (item) {
      // action绑定的数据对象, index是索引
      if (item.name === '反馈垃圾内容') {
        // 证明点击的是反馈垃圾内容
        this.actions = secondActions
        this.cancelText = '返回'
      } else if (item.name === '不感兴趣') {
        this.$emit('dislikeEvent', this.artObj.art_id)
        this.show = false
      } else {
        this.$emit('reportEvent', this.artObj.art_id, item.value)
        this.action = firstActions
        this.show = false
      }
    },
    // 反馈- 取消事件
    onCancel () {
      if (this.cancelText === '返回') {
        this.actions = firstActions
        this.cancelText = '取消'
        this.show = true // 点返回不要让弹窗关闭, 点取消文字按钮, 弹窗默认关闭
      }
    },
    // 反馈- 关闭面板
    onClose () {
      this.actions = firstActions
      this.cancelText = '取消'
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-st;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
